package com.tju.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.Result;
import com.tju.eduservice.entity.EduCourse;
import com.tju.eduservice.entity.vo.CourseInfoVo;
import com.tju.eduservice.entity.vo.CoursePublishVO;
import com.tju.eduservice.entity.vo.CourseQuery;
import com.tju.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @PostMapping("addCourse")
    public Result addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }

    //根据id查询课程
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourse")
    public Result updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);

        return Result.ok();
    }
    //根据课程id查询确认的所有信息
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id){
        CoursePublishVO courseInfo = courseService.publishCourseInfo(id);
        return Result.ok().data("publishInfo",courseInfo);
    }
    //课程最终发布，将其状态由draft改为normal
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return  Result.ok();
    }
    //获取课程列表
    @GetMapping("getCourseList")
    public Result getCourseList(){
        List<EduCourse> list = courseService.list();
        return Result.ok().data("courseList",list);
    }

    //条件查询列表 查询课程带分页
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public Result paCourseCondition(@PathVariable long page,
                                    @PathVariable long limit,
                                    @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>();
        courseService.pageQuery(pageParam,courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return Result.ok().data("total",total).data("rows",records);
    }
    //课程列表中的删除方法
    @DeleteMapping("/removeCourseById/{id}")
    public Result removeCourseById(@PathVariable String id){
        boolean flag = courseService.removeCourse(id);
        if(flag){
            return  Result.ok();
        }else {
            return Result.error();
        }

    }

}

